#!/bin/bash
# author    tyb

# 函数
function isgreen() {
    if [ $? -eq 0 ]; then
        echo -e "\e[32m$1==========>succeed\e[0m"
    else
        echo -e "\e[31m$1==========>failed\e[0m"
        exit 1
    fi
}

function print_help() {
  echo "Usage: $0 [OPTIONS]"
  echo "Options:"
  echo "  --help            Display this help message."
  echo "  --total           Slot total quantity"
  echo "  --ssd             Ssd disk quantity"
  echo "  --nvme            Nvme disk name"
  echo "  --nvme_size       Nvme size"
  echo "  --back | B        List of back-end storage disks"
  echo "  --real            The --real parameter can be 'yes' or 'no',to select whether to display or execute the command default: 'no'"
  echo "  --partition_size  Specify partition size default: 40G"


  echo "example:"
  echo "  --total 12 --ssd 2 --nvme nvme0n1 --nvme_size 1500 -B \"sdb sdc sdd...\""
  echo "  ps: -B Parameter format:\"\" "
}


while [[ $# -gt 0 ]]; do
  case "$1" in
    --help)
       print_help
        exit 0
        ;;
    --back|-B)
      if [[ -n "$2" ]]; then
        back_device="$2"
        shift 2
      else
        echo "Missing argument for --help option."
        exit 1
      fi
      ;;
    --total)
      if [[ -n "$2" ]]; then
        total="$2"
        shift 2
      else
        echo "Missing argument for --help option."
        exit 1
      fi
      ;;
    --ssd)
      if [[ -n "$2" ]]; then
        ssd="$2"
        shift 2
      else
        echo "Missing argument for --help option."
        exit 1
      fi
      ;;
    --nvme)
      if [[ -n "$2" ]]; then
        nvme="$2"
        shift 2
      else
        echo "Missing argument for --help option."
        exit 1
      fi
      ;;
    --nvme_size)
      if [[ -n "$2" ]]; then
        nvme_size="$2"
        shift 2
      else
        echo "Missing argument for --help option."
        exit 1
      fi
      ;;
    --real)
      if [[ -n "$2" ]]; then
        real="$2"
        shift 2
      else
        echo "Missing argument for --help option."
        exit 1
      fi
      ;;
    --partition_size)
      if [[ -n "$2" ]]; then
        partition_size="$2"
        shift 2
      else
        echo "Missing argument for --help option."
        exit 1
      fi
      ;;
    *)
      echo "Invalid option: $1"
      exit 1
      ;;
  esac
done


# 变量
# total=12
# ssd=3
# hdd=5

# nvme=nvme0n1
# nvme_size=1500
real_value=${real:-"no"}
partition=${partition_size:-40}



# 分区
if [ "$real_value" == "no" ]; then
  echo "parted  /dev/${nvme}  mklabel gpt  -s"
elif [ "$real_value" == "yes" ]; then
  parted  /dev/"${nvme}"  mklabel gpt  -s
else
    echo "The --real parameter can be 'yes' or 'no'"
fi
# 定义起始值和结束值
start=1
end=$(($(($((total-ssd-1))*partition))+1))
step=$partition

for i in $(seq $start "$step" $end); do
  if [ "$real_value" == "no" ]; then
    echo "parted /dev/${nvme} mkpart primary ${i}Gib $((i+partition))Gib"
  elif [ "$real_value" == "yes" ]; then
    parted /dev/"${nvme}" mkpart primary "${i}"Gib $((i+partition))Gib
    isgreen "创建分区"
    sleep 1
  else
    echo "The --real parameter can be 'yes' or 'no'"
  fi
  num=$((num+1))
#   sleep 1
done

echo ""
l2_start=$(($(($((total-ssd))*partition))+10))
l2_step=$(($((nvme_size-l2_start))/$((total-ssd))))
l2_end=$((nvme_size-l2_step))
for j in $(seq $l2_start $l2_step $l2_end); do
    # time=$((time+1))
    if [ $((j+l2_step)) -gt $((nvme_size-10)) ]; then
      if [ "$real_value" == "no" ]; then
        echo "parted /dev/${nvme} mkpart primary ${j}Gib $((nvme_size-10))Gib"
      elif [ "$real_value" == "yes" ]; then
        parted /dev/"${nvme}" mkpart primary "${j}"Gib $((nvme_size-10))Gib
        isgreen "创建分区"
        sleep 1
      else
        echo "The --real parameter can be 'yes' or 'no'"
      fi
    else
      if [ "$real_value" == "no" ]; then
        echo "parted /dev/${nvme} mkpart primary ${j}Gib $((j+l2_step))Gib"
      elif [ "$real_value" == "yes" ]; then
        parted /dev/"${nvme}" mkpart primary "${j}"Gib $((j+l2_step))Gib
        isgreen "创建分区"
        sleep 1
      else
        echo "The --real parameter can be 'yes' or 'no'"
      fi
        # sleep 1
    fi
done

# 添加后端存储盘
# echo ""
# echo "${back_device}"
# for k in ${back_device}; do
#     echo "make-bcache -B /dev/${k}"
# done

# 添加缓存盘
echo ""
# echo "$num"
num1=$num
for k in ${back_device}; do
    num1=$((num1+1))
    if [ "$real_value" == "no" ]; then
      echo "make-bcache -C /dev/${nvme}p${num1} -B /dev/${k} --wipe-bcache --discard --writeback"
    elif [ "$real_value" == "yes" ]; then
      make-bcache -C /dev/"${nvme}"p"${num1}" -B /dev/"${k}" --wipe-bcache --discard --writeback
      isgreen "创建bcache /dev/${nvme}p${num1} --- /dev/${k} "
      sleep 1
    else
      echo "The --real parameter can be 'yes' or 'no'"
    fi
done

# 
# echo ""
# n=0
# num2=$num
# for k in ${back_device}; do
#     num2=$((num2+1))    
#     echo "bcache-super-show /dev/${nvme}p${num2} |grep cset.uuid |awk '{print \$2}' > /sys/block/bcache${n}/bcache/attach"
#     n=$((n+1))
# done

#
# echo ""
# m=0
# for k in ${back_device}; do
#     echo "echo writeback >/sys/block/bcache${m}/bcache/cache_mode"
#     m=$((m+1))
# done